const router = require('koa-router')();
const mysqlHelper = require('../libs/MySQLHelper');
const scripts = require('../configs/scripts/msyql');

router.prefix('/api/Tenant');

router.post('/SearchTenant', async (ctx, next) => {
    await mysqlHelper.queryForList(scripts.SearchTenantTransportData, ctx.request.body,
        builder => builder.and('GUID=@Guid')
            .and('ChineseName like concat("%", @ChineseName ,"%")')
            .and('HelpCode like concat("%", @HelpCode, "%")')
            .and('TenantType=@TenantType')
            .and('BizState=@BizState')
            .and('IsOperateTeam=@IsOperateTeam')
            .and('IsAccountOpen=@IsAccountOpen')
            .and('ConfigRights=@ConfigRights')
            .and('ApplySource=@ApplySource')
            .and('Classification=@Classification')
            .and('IsDeleted=@IsDeleted')
            .and('IsAudit=@IsAudit')
            .and('ExpireDate=@ExpireDate')
            .and('totalAreaage IS NOT NULL', ()=>true)
            .and('Classification = "0"', ()=>true)
            .desc('TotalAreaAge')
            .paging()
    ).then(rs => {
        if (rs) {
            rs.remoteUrl = ctx.request.origin;
        }
        ctx.body = rs;
    }, reason => ctx.body = reason);
})


module.exports = app => {
    app.use(router.routes(), router.allowedMethods());

    return router;
};